# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from kafka import KafkaProducer
import json


class MyspiderPipeline(object):
    """
    按照kafka

    pip install kafka
    pip install kafka-python

    """

    def __init__(self):
        self.producer = KafkaProducer(bootstrap_servers="node2:9092,node3:9092,node4:9092")

    def process_item(self, item, spider):
        # 将数据转换成json 字符串
        data = json.dumps(dict(item), ensure_ascii=False)

        # 数据打入kafka
        self.producer.send(topic="python", value=data.encode("utf-8"))
